package com.itHuangTao.tank.collisions;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.dsl.components.EffectComponent;
import com.almasb.fxgl.dsl.components.HealthIntComponent;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.physics.CollisionHandler;
import com.itHuangTao.tank.GameType;
import com.itHuangTao.tank.ItemType;
import com.itHuangTao.tank.TankApp;
import com.itHuangTao.tank.components.TankLevelComponent;
import com.itHuangTao.tank.effects.FreezeEffect;
import com.itHuangTao.tank.effects.HelmetEffect;
import com.itHuangTao.tank.effects.ShipEffect;

import static com.almasb.fxgl.dsl.FXGL.spawn;

public class ItemEnemyHandler extends CollisionHandler {

    public ItemEnemyHandler() {
        super(GameType.ITEM, GameType.ENEMY);
    }

    @Override
    protected void onCollisionBegin(Entity item, Entity enemy) {
        //获得道具的类型
        ItemType itemType = item.getObject("itemType");

        TankApp.PropCount--;
        //对道具的类型进行选择判断
        switch (itemType) {
            //星星和枪：敌人已经产生的坦克的等级满级
            case STAR, GUN -> {
                FXGL.getGameWorld()
                        .getEntitiesByType(GameType.ENEMY)
                        .forEach(entity -> {
                            entity.getComponent(TankLevelComponent.class).restoreFully();
                        });
            }
            //坦克，玩家生命值减一
            case TANK -> {
                FXGL.getGameWorld().getEntitiesByType(GameType.PLAYER).forEach(player -> {
                    if (player.isActive()) {
                        HealthIntComponent hp = player.getComponent(HealthIntComponent.class);
                        hp.damage(1);
                        //玩家血量为0的时候
                        if (hp.isZero()) {
                            spawn("explode", player.getCenter()
                                    .subtract(50 / 2.0, 50 / 2.0));//在坦克中心爆炸
                            player.removeFromWorld();//移除玩家
                            //弹出失败场景
                            FXGL.<TankApp>getAppCast().gameOver();
                        }
                    }
                });
            }
            //爱心，玩家生命值减少一半
            case HEART -> {
                FXGL.getGameWorld().getEntitiesByType(GameType.PLAYER).forEach(player -> {
                    if (player.isActive()) {
                        HealthIntComponent hp = player.getComponent(HealthIntComponent.class);
                        hp.damage(hp.getValue() / 2);
                        //玩家血量为0的时候
                        if (hp.isZero()) {
                            spawn("explode", player.getCenter()
                                    .subtract(50 / 2.0, 50 / 2.0));//在坦克中心爆炸
                            player.removeFromWorld();//移除玩家
                            //弹出失败场景
                            FXGL.<TankApp>getAppCast().gameOver();
                        }
                    }
                });
            }
            //炸弹，使得玩家只剩下一点血
            case BOMB -> {
                FXGL.getGameWorld().getEntitiesByType(GameType.PLAYER).forEach(player -> {
                    if (player.isActive()) {
                        HealthIntComponent hp = player.getComponent(HealthIntComponent.class);
                        hp.setValue(1);
                    }
                });
            }
            //轮船道具，可以使得已经产生的坦克在水面上行走
            case SHIP -> {
                FXGL.getGameWorld()
                        .getEntitiesByType(GameType.ENEMY)
                        .forEach(entity -> {
                            entity.getComponent(EffectComponent.class).startEffect(new ShipEffect());
                        });
            }
            //时间，可以使得我方坦克静止4秒钟
            case TIME -> {
                FXGL.getGameWorld().getEntitiesByType(GameType.PLAYER).forEach(player -> {
                    if (player.isActive()) {
                        player.getComponent(EffectComponent.class).startEffect(new FreezeEffect());
                    }
                });
            }
            //钢盔，可以使得敌方坦克无敌10秒
            case HELMET -> {
                enemy.getComponent(EffectComponent.class).startEffect(new HelmetEffect());
            }
            //铲子，可以升级基地的保护墙
            case SPADE -> {
                FXGL.<TankApp>getAppCast().unReinforce();
            }
        }

        //移除道具
        item.removeFromWorld();
        FXGL.play("item.wav");
    }
}
